package com.lz.ionicbase.view;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lz.ionicbase.R;
import com.lz.ionicbase.model.GridListBean;
import com.lz.ionicbase.ui.fragment.AppFragment;
import com.lz.ionicbase.utils.BaseViewHolder;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;

import java.util.List;

/**
 * Created by lenovo on 2017/2/5.
 */
public class MyGridAdapter extends BaseAdapter {
    private AppFragment appFragment;
    private  List<GridListBean> list;
    private  Context context;

    private boolean isShowDelete;//根据这个变量来判断是否显示删除图标
    private boolean isViewbottom;
    private boolean isViewright;
    private int i = 0;//删除个数


    public MyGridAdapter(Context context, List<GridListBean> list, AppFragment appFragment) {
        super();
        this.context = context;
        this.list = list;
        this.appFragment = appFragment;
    }

    /**
     * 判断是否显示删除图标
     * @param isShowDelete
     */
    public void setShowDelete(boolean isShowDelete){
        this.isShowDelete=isShowDelete;
        notifyDataSetChanged();
    }

    /**
     * 获取删除图标显示状态
     * @return
     */
    public boolean getShowDelete(){
        return isShowDelete;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_app_grid, parent, false);
        }
        TextView tv = BaseViewHolder.get(convertView, R.id.grid_tv_item);
        ImageView iv = BaseViewHolder.get(convertView, R.id.grid_iv_item);
        ImageView delete = BaseViewHolder.get(convertView, R.id.grid_iv_delete);
        CheckBox check = BaseViewHolder.get(convertView, R.id.cb_add);
        View viewbottom = BaseViewHolder.get(convertView, R.id.grid_view_bottom);
        View viewright = BaseViewHolder.get(convertView, R.id.grid_view_right);
        if (position==list.size()-1||position==list.size()-2||position==list.size()-3||position==list.size()-4){
            viewbottom.setVisibility(View.VISIBLE);
        }
        if (position==list.size()-1){
            viewright.setVisibility(View.VISIBLE);
        }
        check.setVisibility(View.GONE);
        GridListBean gridListBean = list.get(position);
        final String iid = gridListBean.getIid();
        String neturl = SpUtils.getString(context, Constant.NET_URL, "");
        delete.setVisibility(isShowDelete?View.VISIBLE:View.GONE);
        if(delete.getVisibility() == View.VISIBLE) {
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Log.d("TAG", "onClick: "+clickItemIndex);
                        SpUtils.saveInt(context, "position" + MyGridAdapter.this.i, position);
                        list.remove(position);
                        MyGridAdapter.this.i++;
                        Log.i("------","Adapter删除个数有"+i+"个");
                        Log.i("------", "AdapterRemanderlist有"+list.size()+"个");
                        SpUtils.saveInt(context, "positioncount" , MyGridAdapter.this.i);
//                        appFragment.getDeleteListener(iid);
                        notifyDataSetChanged();
                    }
                });
        }
        Glide.with(context)
                .load(neturl+gridListBean.getSmpath())
//                .diskCacheStrategy( DiskCacheStrategy.NONE )
//                .skipMemoryCache(true)
                .error(R.mipmap.contract)
                .into(iv);
        tv.setText(gridListBean.getSname());
        return convertView;
    }
    public int getDeleteCount(){
        return i;
    }

}
